﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMSservice;
using System.Messaging;

namespace MessageClient
{
    public class messageClient
    {
        MessageQueue inQueue = null;
        MessageQueue outQueue = null;
        public messageClient()
        {
            initQueue();
        }
        private void initQueue()
        {
            // queue for message receive
            if (MessageQueue.Exists(serviceConstant.IN_MESSAGE_QUEUE))
                inQueue = new MessageQueue(serviceConstant.IN_MESSAGE_QUEUE);
            else
                inQueue = MessageQueue.Create(serviceConstant.IN_MESSAGE_QUEUE);
            // queue for message send
            if (MessageQueue.Exists(serviceConstant.OUT_MESSAGE_QUEUE))
                outQueue = new MessageQueue(serviceConstant.OUT_MESSAGE_QUEUE);
            else
                outQueue = MessageQueue.Create(serviceConstant.OUT_MESSAGE_QUEUE);
        }
        public void sendMessage(string phoneNumber,string msgTexts)
        {
            smsClass message = new smsClass(phoneNumber, msgTexts);
            outQueue.Send(message);
        }
        public ShortMessage readMessage()
        {
            ShortMessage message = null;
            try
            {
                Message sms = inQueue.Receive();
                if (sms != null)
                {
                    message = sms.Body as ShortMessage;
                }
                return message;
            }
            catch (Exception e)
            {
                return null;
            }
        }
    }
}
